﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentApartMentManage
{
    public partial class ElectricityPay : Form
    {
        public ElectricityPay()
        {
            InitializeComponent();
        }

        private void ElectricityPay_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DataShow.show("select * from 公寓缴费管理表");
            comboBox1.ValueMember = "公寓楼号";
            comboBox1.DisplayMember = "公寓楼号";
            comboBox1.DataSource = sqlHelper.GetTable("select distinct 公寓楼号 from 公寓楼房信息表");
            comboBox2.ValueMember = "公寓号";
            comboBox2.DisplayMember = "公寓号";
            comboBox2.DataSource = sqlHelper.GetTable("select distinct 公寓号 from 公寓信息表");
            comboBox3.ValueMember = "学号";
            comboBox3.DisplayMember = "学号";
            comboBox3.DataSource = sqlHelper.GetTable("select distinct 学号 from 学生信息表");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要清空输入的信息吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                comboBox1.Text = "";
                comboBox2.Text = "";
                comboBox3.Text = "";
                domainUpDown1.Text = "0";

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if(MessageBox.Show("你真的要给"+comboBox1.Text.Trim()+"栋"+comboBox2.Text.Trim()+"寝室充值"+domainUpDown1.Text.Trim()+"元电费吗?","提示",MessageBoxButtons.YesNo)==DialogResult.Yes)
                {
                    string sql = string.Format("insert into 公寓缴费管理表(公寓楼号,公寓号,缴费人ID,缴费金额) values('{0}','{1}','{2}',{3})", comboBox1.Text.Trim(), comboBox2.Text.Trim(), comboBox3.Text.Trim(),Convert.ToInt32(domainUpDown1.Text.Trim()));
                    int i = sqlHelper.Execute(sql);
                    if (i != -1)
                    {
                        MessageBox.Show("电费充值成功!");
                    }
                    else
                        MessageBox.Show("电费充值失败!");
                    dataGridView1.DataSource = DataShow.show("select * from 公寓缴费管理表");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("充值失败!");
                return;
            }
        }

        private void ElectricityPay_MouseClick(object sender, MouseEventArgs e)
        {
            
        }

        private void ElectricityPay_MouseClick_1(object sender, MouseEventArgs e)
        {

        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                //右键选中单元格
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;

                //在单元格处弹出菜单
                contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
            }
        }

        private void 电费查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DataShow.show("select * from 公寓电费管理表");
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DataShow.show("select * from 公寓缴费管理表");
        }
    }
}
